package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> pageQury(SetmealPageQueryDTO pageQueryDTO) {
        log.info("传递来的信息是{}",pageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除的id是{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("要查询的id是{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     */
    @PutMapping
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result UpdateById(@RequestBody SetmealDTO setmealDTO) {
        log.info("传递来的信息是{}",setmealDTO);
        setmealService.updateById(setmealDTO);
        return Result.success();
    }
    /**
     * 套餐起售停售
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMeal",key = "#id")
    public Result startOrStop(@PathVariable Integer status , Long id) {
        log.info("传递来的id是{}，状态是{}",id,status);
        SetmealDTO setmealDTO =SetmealDTO.builder().status(status).id(id).build();
        setmealService.updateById(setmealDTO);
        return Result.success();
    }

    /**
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setMeal",key = "#setmealDTO.categoryId")
    public Result setMealInsert(@RequestBody SetmealDTO setmealDTO) {
        log.info("传递来的信息是{}",setmealDTO);
        setmealService.setMealInsert(setmealDTO);
        return Result.success();

    }
}
